package com.cykj.pos.service.impl;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.TransactionDetailsDTO;
import com.cykj.pos.mapper.BizMerchantMapper;
import com.cykj.pos.service.IBizMerchantService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.AizProfitSharingRecordMapper;
import com.cykj.pos.service.IAizProfitSharingRecordService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分润记录Service业务层处理
 *
 * @author weijianbo
 * @date 2025-01-22
 */
@Service
public class AizProfitSharingRecordServiceImpl extends ServiceImpl<AizProfitSharingRecordMapper, AizProfitSharingRecord> implements IAizProfitSharingRecordService {

    @Resource
    private AizProfitSharingRecordMapper aizProfitSharingRecordMapper;

    @Resource
    private IBizMerchantService iBizMerchantService;

    @Resource
    private BizMerchantMapper bizMerchantMapper;

    @Override
    public List<AizProfitSharingRecord> queryList(AizProfitSharingRecord aizProfitSharingRecord) {
//        LambdaQueryWrapper<AizProfitSharingRecord> lqw = Wrappers.lambdaQuery();
//        if (aizProfitSharingRecord.getMerchId() != null){
//            lqw.eq(AizProfitSharingRecord::getMerchId ,aizProfitSharingRecord.getMerchId());
//        }
//        if (aizProfitSharingRecord.getUuid() != null){
//            lqw.eq(AizProfitSharingRecord::getUuid ,aizProfitSharingRecord.getUuid());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getMerchName())){
//            lqw.like(AizProfitSharingRecord::getMerchName ,aizProfitSharingRecord.getMerchName());
//        }
//        if (aizProfitSharingRecord.getUserId() != null){
//            lqw.eq(AizProfitSharingRecord::getUserId ,aizProfitSharingRecord.getUserId());
//        }
//        if (aizProfitSharingRecord.getAmnount() != null){
//            lqw.eq(AizProfitSharingRecord::getAmnount ,aizProfitSharingRecord.getAmnount());
//        }
//        if (aizProfitSharingRecord.getAddiAmount() != null){
//            lqw.eq(AizProfitSharingRecord::getAddiAmount ,aizProfitSharingRecord.getAddiAmount());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getFeeName())){
//            lqw.like(AizProfitSharingRecord::getFeeName ,aizProfitSharingRecord.getFeeName());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getPosSn())){
//            lqw.eq(AizProfitSharingRecord::getPosSn ,aizProfitSharingRecord.getPosSn());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getMerchantNo())){
//            lqw.eq(AizProfitSharingRecord::getMerchantNo ,aizProfitSharingRecord.getMerchantNo());
//        }
//        if (aizProfitSharingRecord.getShareAmount() != null){
//            lqw.eq(AizProfitSharingRecord::getShareAmount ,aizProfitSharingRecord.getShareAmount());
//        }
//        if (aizProfitSharingRecord.getSettlementRate() != null){
//            lqw.eq(AizProfitSharingRecord::getSettlementRate ,aizProfitSharingRecord.getSettlementRate());
//        }
//        if (aizProfitSharingRecord.getHostDate() != null){
//            lqw.eq(AizProfitSharingRecord::getHostDate ,aizProfitSharingRecord.getHostDate());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getRemake())){
//            lqw.eq(AizProfitSharingRecord::getRemake ,aizProfitSharingRecord.getRemake());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getChannel())){
//            lqw.eq(AizProfitSharingRecord::getChannel ,aizProfitSharingRecord.getChannel());
//        }
//        if (aizProfitSharingRecord.getManufacturerId() != null){
//            lqw.eq(AizProfitSharingRecord::getManufacturerId ,aizProfitSharingRecord.getManufacturerId());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getManufacturerName())){
//            lqw.like(AizProfitSharingRecord::getManufacturerName ,aizProfitSharingRecord.getManufacturerName());
//        }
//        if (aizProfitSharingRecord.getModelId() != null){
//            lqw.eq(AizProfitSharingRecord::getModelId ,aizProfitSharingRecord.getModelId());
//        }
//        if (StringUtils.isNotBlank(aizProfitSharingRecord.getModelName())){
//            lqw.like(AizProfitSharingRecord::getModelName ,aizProfitSharingRecord.getModelName());
//        }
//        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
//        if (sysUserman.getUserId()==1l){
//            sysUserman.setUserId(2l);
//        }
//        BizMerchant bizMerchant = bizMerchantMapper.selectInfoByUserId(sysUserman.getUserId());
//        List<Long> list = bizMerchantMapper.selectChildMerchList(bizMerchant.getMerchId());
//        list.add(bizMerchant.getMerchId());
//        lqw.in(AizProfitSharingRecord::getMerchId,list);
//        lqw.orderByDesc(AizProfitSharingRecord::getHostDate);
//        return this.list(lqw);
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        aizProfitSharingRecord.setDeptId(sysUserman.getDeptId());
        return aizProfitSharingRecordMapper.queryList(aizProfitSharingRecord);
    }

    @Override
    public Double selectShare(TransactionDetailsDTO detailsDTO) {
        return aizProfitSharingRecordMapper.selectShare(detailsDTO);
    }

    @Override
    public List<BizMerchantTranStatistics> queryTransRecord(BizMerchantTranStatistics bizMerchantTranStatistics) {
        if(bizMerchantTranStatistics.getParams().get("beginTime") != null){
            bizMerchantTranStatistics.setBeginTime(bizMerchantTranStatistics.getParams().get("beginTime").toString()+" 00:00:00");
            bizMerchantTranStatistics.setEndTime(bizMerchantTranStatistics.getParams().get("endTime").toString()+" 23:59:59");
        }
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        bizMerchantTranStatistics.setDeptId(sysUserman.getDeptId());
        List<BizMerchantTranStatistics> list = aizProfitSharingRecordMapper.queryTransRecord(bizMerchantTranStatistics);
        return list;
    }

    @Override
    public List<BizMerchantTranStatisticsMerchantExport> queryMerchRecord(BizMerchantTranStatisticsMerchantExport bizMerchantTranStatisticsMerchantExport) {
        if(bizMerchantTranStatisticsMerchantExport.getParams().get("beginTime") != null){
            bizMerchantTranStatisticsMerchantExport.setBeginTime(bizMerchantTranStatisticsMerchantExport.getParams().get("beginTime").toString()+" 00:00:00");
            bizMerchantTranStatisticsMerchantExport.setEndTime(bizMerchantTranStatisticsMerchantExport.getParams().get("endTime").toString()+" 23:59:59");
        }
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        bizMerchantTranStatisticsMerchantExport.setDeptId(sysUserman.getDeptId());
        List<BizMerchant> merchantList =  aizProfitSharingRecordMapper.queryMyDeptList(bizMerchantTranStatisticsMerchantExport);
        List<BizMerchantTranStatisticsMerchantExport> list = new ArrayList<>();
        for(BizMerchant mer : merchantList){
            if(mer.getMerchId() == 1L || mer.getMerchId() == 2L){
                continue;
            }
            BizMerchantTranStatisticsMerchantExport btsme = new BizMerchantTranStatisticsMerchantExport();
            List<Long> childIds =  aizProfitSharingRecordMapper.getChilds(mer.getMerchId());
            childIds.add(mer.getMerchId());
            bizMerchantTranStatisticsMerchantExport.setChilds(childIds);
            Double transAmount = aizProfitSharingRecordMapper.getTransAmount(bizMerchantTranStatisticsMerchantExport);
            btsme.setMerchName(mer.getMerchName());
            btsme.setTransAmountA(transAmount);
            bizMerchantTranStatisticsMerchantExport.setMerchId(mer.getMerchId());
            btsme.setShareAmountA(aizProfitSharingRecordMapper.getShareAmount(bizMerchantTranStatisticsMerchantExport));
            list.add(btsme);
        }
//        List<BizMerchantTranStatisticsMerchantExport> list = aizProfitSharingRecordMapper.queryMerchRecord(bizMerchantTranStatisticsMerchantExport);
        return list;
    }

    @Override
    public List<BizMerchantTranStatisticsWithdrawalExport> withdrawalRecord(BizMerchantTranStatisticsWithdrawalExport bizMerchantTranStatisticsWithdrawalExport) {
        if(bizMerchantTranStatisticsWithdrawalExport.getParams().get("beginTime") != null){
            bizMerchantTranStatisticsWithdrawalExport.setBeginTime(bizMerchantTranStatisticsWithdrawalExport.getParams().get("beginTime").toString()+" 00:00:00");
            bizMerchantTranStatisticsWithdrawalExport.setEndTime(bizMerchantTranStatisticsWithdrawalExport.getParams().get("endTime").toString()+" 23:59:59");
        }
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        if(sysUserman.getDeptId() == 100L){

        }else if(sysUserman.getDeptId() == 101L){
            bizMerchantTranStatisticsWithdrawalExport.setRemark("安心付-伙伴");
        }else{
            bizMerchantTranStatisticsWithdrawalExport.setRemark("安心付-乐富");
        }
        List<BizMerchantTranStatisticsWithdrawalExport> list = aizProfitSharingRecordMapper.queryWithdrawalRecord(bizMerchantTranStatisticsWithdrawalExport);
        return list;
    }

    @Override
    public AjaxResult revenueStatistics(BizMerchantTranStatistics bizMerchantTranStatistics) {
        AjaxResult ajaxResult = AjaxResult.success("结果集");
        if(bizMerchantTranStatistics.getParams().get("beginTime") != null){
            bizMerchantTranStatistics.setBeginTime(bizMerchantTranStatistics.getParams().get("beginTime").toString()+" 00:00:00");
            bizMerchantTranStatistics.setEndTime(bizMerchantTranStatistics.getParams().get("endTime").toString()+" 23:59:59");
        }
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        bizMerchantTranStatistics.setDeptId(sysUserman.getDeptId());
        //激活
        ajaxResult.put("activeCount",0);
        ajaxResult.put("activeAmount",0);
        //返现
        bizMerchantTranStatistics.setAmtType("2");
        BizMerchantTranStatistics cashback = aizProfitSharingRecordMapper.queryCashback(bizMerchantTranStatistics);
        ajaxResult.put("cashbackCount",cashback.getCashbackCount());
        ajaxResult.put("cashbackAmount",cashback.getCashbackAmount());
        //流量费
        bizMerchantTranStatistics.setAmtType("0");
        BizMerchantTranStatistics flowFee = aizProfitSharingRecordMapper.queryCashback(bizMerchantTranStatistics);
        ajaxResult.put("flowFeeCount",flowFee.getCashbackCount());
        ajaxResult.put("flowFeeAmount",flowFee.getCashbackAmount());
        //押金
        bizMerchantTranStatistics.setAmtType("1");
        BizMerchantTranStatistics seriviceFee = aizProfitSharingRecordMapper.queryCashback(bizMerchantTranStatistics);
        ajaxResult.put("serviceFeeCount",seriviceFee.getCashbackCount());
        ajaxResult.put("serviceFeeAmount",seriviceFee.getCashbackAmount());
        //扣款
        BizMerchantTranStatistics deduction = aizProfitSharingRecordMapper.queryData(bizMerchantTranStatistics);
        ajaxResult.put("deductionCount",deduction.getDeductionCount());
        ajaxResult.put("deductionAmount",deduction.getDeductionAmount());
        if(sysUserman.getDeptId() == 100L){
            bizMerchantTranStatistics.setRemark("安心付-闯业");
        }else if(sysUserman.getDeptId() == 101L){
            bizMerchantTranStatistics.setRemark("安心付-伙伴");
        }else{
            bizMerchantTranStatistics.setRemark("安心付-乐富");
        }
        //分润提现统计
        BizMerchantTranStatistics share = aizProfitSharingRecordMapper.queryShareMoneyData(bizMerchantTranStatistics);
        ajaxResult.put("shareWithdrawalCount",share.getShareWithdrawalCount());
        ajaxResult.put("shareWithdrawalAmount",share.getShareWithdrawalAmount());
        ajaxResult.put("shareAchieveAmount",share.getShareAchieveAmount());
        ajaxResult.put("shareIncomeAmount",share.getShareIncomeAmount());
        //机具返现提现统计
        BizMerchantTranStatistics equip = aizProfitSharingRecordMapper.queryEquipMoneyData(bizMerchantTranStatistics);
        ajaxResult.put("equipWithdrawalCount",equip.getEquipWithdrawalCount());
        ajaxResult.put("equipWithdrawalAmount",equip.getEquipWithdrawalAmount());
        ajaxResult.put("equipAchieveAmount",equip.getEquipAchieveAmount());
        ajaxResult.put("equipIncomeAmount",equip.getEquipIncomeAmount());
        //奖励提现统计
        BizMerchantTranStatistics reward = aizProfitSharingRecordMapper.queryRewardMoneyData(bizMerchantTranStatistics);
        ajaxResult.put("rewardWithdrawalCount",reward.getRewardWithdrawalCount());
        ajaxResult.put("rewardWithdrawalAmount",reward.getRewardWithdrawalAmount());
        ajaxResult.put("rewardAchieveAmount",reward.getRewardAchieveAmount());
        ajaxResult.put("rewardIncomeAmount",reward.getRewardIncomeAmount());
        return ajaxResult;
    }
}
